package com.rpc.netty.heartbeatDetectionByNetty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @author 何章怀晓 hezhanghuaixiao
 * @version V1.0
 * @desc
 */
public class HeartbeatDetectionHandler extends ChannelInboundHandlerAdapter {


  //心跳事件处理
  @Override
  public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    //前者是否是后者的类型
    if (evt instanceof IdleStateEvent) {

      IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
      String eventType = null;
      switch (idleStateEvent.state()) {
        case READER_IDLE:
          eventType = "读空闲";
          break;
        case WRITER_IDLE:
          eventType = "写空闲";
          break;
        case ALL_IDLE:
          eventType = "全空闲";
          break;
        default:
          System.out.println("默认返回");

      }

      System.out.println("[客户端]" + ctx.channel().remoteAddress() + "发生了超时事件" + eventType);

    }

  }
}
